import matplotlib.pyplot as plt
import cv2
from skimage.feature import hog
from skimage import data, exposure
import numpy as np
from PIL import Image
from google.colab.patches import cv2_imshow
# Harris Corner Detection
img = cv2.imread('/content/chessboard.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
gray = np.float32(gray)
dst = cv2.cornerHarris(gray,2,3,0.04)
dst = cv2.dilate(dst,None)
img[dst>0.01*dst.max()]=[0,0,255]
#cv2.imshow('dst',img)
plt.imshow(dst),plt.show()
#FOR SIFT (Since it has patent)
!pip install opencv-python==3.3.0.10 opencv-contrib-python==3.3.0.10
#SIFT
img= cv2.imread('/content/sample.jpg')
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
sift=cv2.xfeatures2d.SIFT_create()
kp=sift.detect(gray,None)
print(kp)
img=cv2.drawKeypoints(gray,kp,img)
cv2.imwrite('/content/abc.jpg',img)
img=cv2.drawKeypoints(gray,kp,img,flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
cv2.imwrite('/content/abc.jpg',img)
sift=cv2.xfeatures2d.SIFT_create
sift_img= cv2.imread('/content/abc.jpg')
plt.imshow(sift_img),plt.show()
#SURF
img= cv2.imread('/content/sample0.jpg')
surf=cv2.xfeatures2d.SURF_create()
kp, des = surf.detectAndCompute(img,None)
print(des)
img2 = cv2.drawKeypoints(img,kp,None,(255,0,0),4)
plt.imshow(img2),plt.show()
# Histogram of gradient
image = cv2.imread('/content/abc.jpg')
fd, hog_image = hog(image, orientations=8, pixels_per_cell=(16, 16),
cells_per_block=(1, 1), visualize=True, multichannel=True)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 10), sharex=True, sharey=True)
ax1.axis('off')
ax1.imshow(image, cmap=plt.cm.gray)
ax1.set_title('Input image')
hog_image_rescaled = exposure.rescale_intensity(hog_image, in_range=(0, 10))
ax2.axis('off')
ax2.imshow(hog_image_rescaled, cmap=plt.cm.gray)
ax2.set_title('Histogram of Oriented Gradients')
plt.show()
win='hog image'
import pickle
import os
import scipy
import scipy.spatial
# Feature extractor
def extract_features(image_path, vector_size=32):
image = cv2.imread(image_path)
try:
alg = cv2.xfeatures2d.SIFT_create()
kps = alg.detect(image)
kps = sorted(kps, key=lambda x: -x.response)[:vector_size]
kps, dsc = alg.compute(image, kps)
dsc = dsc.flatten()
needed_size = (vector_size * 64)
if dsc.size < needed_size:
dsc = np.concatenate([dsc, np.zeros(needed_size - dsc.size)])
except cv2.error as e:
print ('Error: ', e)
return None
return dsc
def batch_extractor(images_path, pickled_db_path="features.pickle"):
files = [os.path.join(images_path, p) for p in sorted(os.listdir(images_path))]
result = {}
for f in files:
print ('Extracting features from image %s' % f)
name = f.split('/')[-1].lower()
result[name] = extract_features(f)
#print(result[name])
# saving all our feature vectors in pickled file
with open(pickled_db_path, 'wb') as fp:
pickle.dump(result, fp)
class Matcher(object):
def __init__(self, pickled_db_path="features.pickle"):
with open(pickled_db_path,'rb') as fp:
self.data = pickle.load(fp)
self.names = []
self.matrix = []
for k, v in self.data.items():
self.names.append(k)
self.matrix.append(v)
self.matrix = np.array(self.matrix)
self.names = np.array(self.names)
def cos_cdist(self, vector):
v = vector.reshape(1, -1)
return scipy.spatial.distance.cdist(self.matrix, v, 'cosine').reshape(-1)
def match(self, image_path, topn=5):
features = extract_features(image_path)
img_distances = self.cos_cdist(features)
# getting top 5 records
nearest_ids = np.argsort(img_distances)[:topn].tolist()
nearest_img_paths = self.names[nearest_ids].tolist()
return nearest_img_paths, img_distances[nearest_ids].tolist()
def show_img(path):
img = cv2.imread(path)
plt.imshow(img)
plt.show()
def run():
images_path = '/content/sample_data/images'
files = [os.path.join(images_path, p) for p in sorted(os.listdir(images_path))]
sample=files
batch_extractor(images_path)
ma = Matcher('features.pickle')
for s in sample:
print ('Query image ================')
show_img(s)
names, match = ma.match(s, topn=3)
print ('Result images ===============')
for i in range(3):
print ('Match %s' % (1-match[i]))
show_img(os.path.join(images_path, names[i]))
run()
# Feature extractor
def extract_features(image_path, vector_size=32):
image = cv2.imread(image_path)
try:
alg = cv2.xfeatures2d.SURF_create()
kps = alg.detect(image)
kps = sorted(kps, key=lambda x: -x.response)[:vector_size]
kps, dsc = alg.compute(image, kps)
dsc = dsc.flatten()
needed_size = (vector_size * 64)
if dsc.size < needed_size:
dsc = np.concatenate([dsc, np.zeros(needed_size - dsc.size)])
except cv2.error as e:
print ('Error: ', e)
return None
return dsc
def batch_extractor(images_path, pickled_db_path="features.pickle"):
files = [os.path.join(images_path, p) for p in sorted(os.listdir(images_path))]
result = {}
for f in files:
print ('Extracting features from image %s' % f)
name = f.split('/')[-1].lower()
result[name] = extract_features(f)
#print(result[name])
# saving all our feature vectors in pickled file
with open(pickled_db_path, 'wb') as fp:
pickle.dump(result, fp)
class Matcher(object):
def __init__(self, pickled_db_path="features.pickle"):
with open(pickled_db_path,'rb') as fp:
self.data = pickle.load(fp)
self.names = []
self.matrix = []
for k, v in self.data.items():
self.names.append(k)
self.matrix.append(v)
self.matrix = np.array(self.matrix)
self.names = np.array(self.names)
def cos_cdist(self, vector):
v = vector.reshape(1, -1)
return scipy.spatial.distance.cdist(self.matrix, v, 'cosine').reshape(-1)
def match(self, image_path, topn=5):
features = extract_features(image_path)
img_distances = self.cos_cdist(features)
# getting top 5 records
nearest_ids = np.argsort(img_distances)[:topn].tolist()
nearest_img_paths = self.names[nearest_ids].tolist()
return nearest_img_paths, img_distances[nearest_ids].tolist()
def show_img(path):
img = cv2.imread(path)
plt.imshow(img)
plt.show()
def run():
images_path = '/content/sample_data/images/'
files = [os.path.join(images_path, p) for p in sorted(os.listdir(images_path))]
sample=files
batch_extractor(images_path)
ma = Matcher('features.pickle')
for s in sample:
print ('Query image ================')
show_img(s)
names, match = ma.match(s, topn=3)
print ('Result images ===============')
for i in range(3):
print ('Match %s' % (1-match[i]))
show_img(os.path.join(images_path, names[i]))
run()